﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infra.Common.DAO
{
    public class UserDAL
    {
        public static void Salvar(Infra.Entity.SEGUSER user)
        {
            using (Infra.Entity.DBInfraEntityDataContext dc = new Entity.DBInfraEntityDataContext())
            {
                if (user.CODUSER != 0)
                {
                    //Alterar o utilizador
                    var u = dc.SEGUSERs.Single(us => us.CODUSER == user.CODUSER);
                    u.STRUSER = user.STRUSER;
                    u.STRPASSWORD = user.STRPASSWORD;
                    dc.SubmitChanges();
                }
                else
                {
                    //Gravar utilizador
                    dc.SEGUSERs.InsertOnSubmit(user);
                    dc.SubmitChanges();
                }
            }
        }

        public static void Excluir(int idUser)
        {
            using (Infra.Entity.DBInfraEntityDataContext dc = new Entity.DBInfraEntityDataContext())
            {
                var u = dc.SEGUSERs.Single(us => us.CODUSER == idUser);
                dc.SEGUSERs.DeleteOnSubmit(u);
                dc.SubmitChanges();
            }
        }

        public static List<Infra.Entity.SEGUSER> Pesquisar(int idUser, string nome)
        {
            List<Infra.Entity.SEGUSER> Users = null;

            using (Infra.Entity.DBInfraEntityDataContext dc = new Entity.DBInfraEntityDataContext())
            {
                var u = from user in dc.SEGUSERs
                        where ((idUser.Equals(0) || idUser.Equals(user.CODUSER)) &&
                        (nome.Equals(string.Empty) || nome.Equals(user.STRUSER.Contains(nome))))
                        select user;
                Users = u.ToList();
            }

            return Users;
        }
    }
}
